package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.back.domain.AppInfo
import com.ruoyi.back.mapper.AppInfoMapper
import com.ruoyi.back.service.AppInfoService
import org.springframework.stereotype.Service

@Service
class AppInfoServiceImpl : ServiceImpl<AppInfoMapper, AppInfo>(), AppInfoService {

    override fun getAppInfo(appInfoSearchDto: AppInfoService.AppInfoSearchDto): Data<AppInfo> =
        Ok("获取app的相关信息成功", getOne(KtQueryWrapper(AppInfo::class.java)
            .eq(AppInfo::type, appInfoSearchDto.type)
            .eq(AppInfo::channelType, appInfoSearchDto.channelType)))

    override fun appInfo(): AppInfo? = getOne(KtQueryWrapper(AppInfo::class.java).orderByDesc(AppInfo::createTime))

    override fun saveAppInfo(appInfo: AppInfo): Data<Boolean> =
        appInfo.id?.run {
            Ok("更新成功", updateById(appInfo))
        } ?: Ok("保存成功", save(appInfo))
}